package cn.chiship.framework.upms.biz.system.service.impl;

import cn.chiship.framework.upms.biz.system.entity.UpmsConfig;
import cn.chiship.framework.upms.biz.system.entity.UpmsConfigExample;
import cn.chiship.framework.upms.biz.system.mapper.UpmsConfigMapper;
import cn.chiship.framework.upms.biz.system.pojo.dto.UpmsConfigGroupDto;
import cn.chiship.framework.upms.biz.system.pojo.dto.UpmsConfigSettingDto;
import cn.chiship.framework.upms.biz.system.service.UpmsConfigService;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.enums.BaseResultEnum;
import cn.chiship.sdk.framework.base.BaseServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统配置业务接口实现层
 * 2021/9/27
 *
 * @author lijian
 */
@Service
public class UpmsConfigServiceImpl extends BaseServiceImpl<UpmsConfig, UpmsConfigExample> implements UpmsConfigService {

    @Resource
    UpmsConfigMapper upmsConfigMapper;

    @Override
    public BaseResult saveGroup(UpmsConfigGroupDto configGroupDto) {
        UpmsConfigExample upmsConfigExample = new UpmsConfigExample();
        upmsConfigExample.createCriteria().andParamCodeEqualTo(configGroupDto.getGroupCode());
        List<UpmsConfig> configs = upmsConfigMapper.selectByExample(upmsConfigExample);
        if (!configs.isEmpty()) {
            return BaseResult.error(BaseResultEnum.EXCEPTION_DATA_BASE_REPEAT, "分组编号已存在，请重新输入");
        }
        UpmsConfig upmsConfig = new UpmsConfig();
        upmsConfig.setParamCode(configGroupDto.getGroupCode());
        upmsConfig.setParamName(configGroupDto.getGroupName());
        upmsConfig.setOrders(configGroupDto.getOrders());
        upmsConfig.setPid(configGroupDto.getPid());
        upmsConfig.setType(Byte.valueOf("0"));
        return super.insertSelective(upmsConfig);
    }

    @Override
    public BaseResult updateGroup(String id, UpmsConfigGroupDto configGroupDto) {
        UpmsConfigExample upmsConfigExample = new UpmsConfigExample();
        upmsConfigExample.createCriteria()
                .andParamCodeEqualTo(configGroupDto.getGroupCode())
                .andIdNotEqualTo(id);
        List<UpmsConfig> configs = upmsConfigMapper.selectByExample(upmsConfigExample);
        if (!configs.isEmpty()) {
            return BaseResult.error(BaseResultEnum.EXCEPTION_DATA_BASE_REPEAT, "分组编号已存在，请重新输入");
        }
        UpmsConfig upmsConfig = new UpmsConfig();
        upmsConfig.setId(id);
        upmsConfig.setParamCode(configGroupDto.getGroupCode());
        upmsConfig.setParamName(configGroupDto.getGroupName());
        upmsConfig.setOrders(configGroupDto.getOrders());
        upmsConfig.setPid(configGroupDto.getPid());
        return super.updateByPrimaryKeySelective(upmsConfig);
    }

    @Override
    public BaseResult insertSelective(UpmsConfig upmsConfig) {
        UpmsConfigExample upmsConfigExample = new UpmsConfigExample();
        upmsConfigExample.createCriteria()
                .andParamCodeEqualTo(upmsConfig.getParamCode());
        List<UpmsConfig> configs = upmsConfigMapper.selectByExample(upmsConfigExample);
        if (!configs.isEmpty()) {
            return BaseResult.error(BaseResultEnum.EXCEPTION_DATA_BASE_REPEAT, "参数项编号已存在，请重新输入");
        }
        return super.insertSelective(upmsConfig);
    }

    @Override
    public BaseResult updateByPrimaryKeySelective(UpmsConfig upmsConfig) {
        UpmsConfigExample upmsConfigExample = new UpmsConfigExample();
        upmsConfigExample.createCriteria()
                .andParamCodeEqualTo(upmsConfig.getParamCode())
                .andIdNotEqualTo(upmsConfig.getId());
        List<UpmsConfig> configs = upmsConfigMapper.selectByExample(upmsConfigExample);
        if (!configs.isEmpty()) {
            return BaseResult.error(BaseResultEnum.EXCEPTION_DATA_BASE_REPEAT, "参数项编号已存在，请重新输入");
        }
        return super.updateByPrimaryKeySelective(upmsConfig);
    }

    @Override
    public BaseResult updateConfig(List<UpmsConfigSettingDto> configSettingDtos) {
        for (UpmsConfigSettingDto configSettingDto : configSettingDtos) {
            UpmsConfig upmsConfig = new UpmsConfig();
            upmsConfig.setId(configSettingDto.getId());
            upmsConfig.setParamValue(configSettingDto.getParamValue());
            upmsConfigMapper.updateByPrimaryKeySelective(upmsConfig);
        }
        return BaseResult.ok();
    }
}
